﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WPFWorkbookDesigner.Addons
{
    public class IniReader
    {
        private string path;

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// INIFile Constructor.
        /// </summary>
        /// <param name="iniPath">INI文件路径</param>
        public IniReader(string iniPath)
        {
            path = iniPath;
        }
        /// <summary>
        /// Write Data to the INI File
        /// </summary>
        /// <param name="section">目标段落</param>
        /// Section name
        /// <param name="key">目标关键词</param>
        /// Key Name
        /// <param name="value">关键词的对应值</param>
        /// Value Name
        public void IniWriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, this.path);
        }

        /// <summary>
        /// Read Data Value From the Ini File
        /// </summary>
        /// <param name="section">目标段落</param>
        /// <param name="key">目标关键词</param>
        /// <returns></returns>
        public string IniReadValue(string section, string key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
            return temp.ToString();

        }
    }
}
